package logic

import (
	"context"
	"errors"
	"user_srv/models/mysql"

	"user_srv/internal/svc"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserRegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRegisterLogic {
	return &UserRegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserRegisterLogic) UserRegister(in *user.UserRegisterRequest) (*user.UserRegisterResponse, error) {
	// todo: add your logic here and delete this line
	u := mysql.User{
		Mobile:   in.Mobile,
		Password: in.Password,
	}
	err := u.UserInfo(in.Mobile)
	if err != nil {
		logx.Error("用户信息错误")
		return nil, errors.New("用户信息错误")
	}
	u.Mobile = in.Mobile
	u.Password = in.Password
	err = u.Create()
	if err != nil {
		logx.Error("创建用户注册信息失败")
		return nil, errors.New("创建用户注册信息失败")
	}

	return &user.UserRegisterResponse{
		UserId: int64(u.ID),
	}, nil
}
